package com.bestcem.xm.om.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.wenjuan.bp.sms.grpc.sms.v1.SendSingleRequest;
import com.wenjuan.bp.sms.grpc.sms.v1.SendSingleResponse;
import com.wenjuan.bp.sms.grpc.sms.v1.SmsServiceGrpc;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 单条发送短信
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/18 15:31
 */
@Slf4j
@Service("omSendSingleGrpcService")
public class SendSingleGrpcService {

    @GrpcClient(GrpcClientNames.XM)
    private SmsServiceGrpc.SmsServiceBlockingStub stub;

    /**
     * 单条发送短信接口
     *
     * @param request
     * @return java.util.List<com.wenjuan.bp.sms.grpc.sms.v1.SendResponseItem>
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2021/12/8 13:37
     */
    public boolean sendSingle(SendSingleRequest request) {
        if (log.isInfoEnabled()) {
            log.info("调用SMS单条发送短信接口，request:{}", request.getPhone());
        }
        // 设置请求参数
        SendSingleResponse response = stub.sendSingle(request);

        // 返回值处理
        if (Code.OK_VALUE == response.getStatus().getCodeValue()) {
            if (log.isInfoEnabled()) {
                log.info("调用SMS单条发送短信接口成功，request:{}", request.getPhone());
            }
            return true;
        }
        log.error("调用SMS单条发送短信接口失败，request:{}", request.getPhone());
        return false;
    }
}
